package com.play.ballen.network

import com.google.gson.Gson
import com.play.ballen.data.LoginResult
import com.play.ballen.data.PImGroup
import com.play.ballen.data.PImGroupStatus
import com.play.ballen.data.UserBean
import com.play.ballen.home.entity.*
import com.play.ballen.mall.entity.MallRecordInfo
import com.play.ballen.mall.entity.MallSaleInfo
import com.play.ballen.mall.entity.RateInfo
import com.play.ballen.me.entity.*
import com.play.ballen.pack.entity.PackBallSellInfo
import com.play.ballen.pack.entity.PackInfo
import com.play.ballen.synthetic.entity.*
import io.reactivex.Observable
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody
import okhttp3.RequestBody.Companion.toRequestBody
import retrofit2.http.QueryMap

class ApiRepository {

    private val mediaType = "application/json;charset=UTF-8".toMediaTypeOrNull()
    private val mediaTypeFormData = "multipart/form-data;charset=UTF-8".toMediaTypeOrNull()
    private val gson = Gson()

    private fun getRequestBody(map: MutableMap<String, Any>): RequestBody {
        return gson.toJson(map).toRequestBody(mediaType)
    }

    fun sendCode(map: MutableMap<String, Any>): Observable<BaseResult<Any?>> {
        return RetrofitManager.getInstance().apiService.sendCode(getRequestBody(map))
    }

    fun register(map: MutableMap<String, Any>): Observable<BaseResult<LoginResult>> {
        return RetrofitManager.getInstance().apiService.register(getRequestBody(map))
    }

    fun login(map: MutableMap<String, Any>): Observable<BaseResult<LoginResult>> {
        return RetrofitManager.getInstance().apiService.login(getRequestBody(map))
    }

    fun loginali(map: MutableMap<String, Any>): Observable<BaseResult<LoginResult>> {
        return RetrofitManager.getInstance().apiService.loginali(getRequestBody(map))
    }
    fun forgetPwd(map: MutableMap<String, Any>): Observable<BaseResult<Any?>> {
        return RetrofitManager.getInstance().apiService.forgetPwd(getRequestBody(map))
    }



    fun getPrivacy(): Observable<BaseResult<String>> {
        return RetrofitManager.getInstance().apiService.getPrivacy()
    }

    fun logout(): Observable<BaseResult<Any?>> {
        return RetrofitManager.getInstance().apiService.logout()
    }

    fun index(): Observable<BaseResult<HomeIndexInfo?>> {
        return RetrofitManager.getInstance().apiService.index()
    }

    fun getArticleList(map: MutableMap<String, Any>): Observable<BaseResult<List<ArticleInfo>?>> {
        return RetrofitManager.getInstance().apiService.getArticleList(map)
    }

    fun getArticleDetail(id: String): Observable<BaseResult<ArticleInfo>> {
        return RetrofitManager.getInstance().apiService.getArticleDetail(id)
    }

    fun getMemberIndex(): Observable<BaseResult<MemberIndexInfo>> {
        return RetrofitManager.getInstance().apiService.getMemberIndex()
    }

    fun getWithDrawInfo(): Observable<BaseResult<WithDrawInfo>> {
        return RetrofitManager.getInstance().apiService.getWithDrawInfo()
    }

    fun commitWithDraw(map: MutableMap<String, Any>): Observable<BaseResult<Any?>> {
        return RetrofitManager.getInstance().apiService.commitWithDraw(getRequestBody(map))
    }

    fun moneyLog(map: MutableMap<String, Any>): Observable<BaseResult<List<MoneyLogInfo>?>> {
        return RetrofitManager.getInstance().apiService.moneyLog(map)
    }

    fun service(): Observable<BaseResult<ServiceInfo>> {
        return RetrofitManager.getInstance().apiService.service()
    }

    fun recharge(): Observable<BaseResult<RechargeInfo>> {
        return RetrofitManager.getInstance().apiService.recharge()
    }

    fun memberEdit(map: MutableMap<String, Any>): Observable<BaseResult<UserInfo>> {
        return RetrofitManager.getInstance().apiService.memberEdit(getRequestBody(map))
    }

    fun extraInfo(type: String): Observable<BaseResult<ExtraInfo>> {
        return RetrofitManager.getInstance().apiService.extraInfo(type)
    }

    fun extraConfirm(map: MutableMap<String, Any>): Observable<BaseResult<Any?>> {
        return RetrofitManager.getInstance().apiService.extraConfirm(getRequestBody(map))
    }

    fun team(@QueryMap params: MutableMap<String, Any>): Observable<BaseResult<TeamInfo>> {
        return RetrofitManager.getInstance().apiService.team(params)
    }

    fun invitePoster(): Observable<BaseResult<InvitePosterInfo>> {
        return RetrofitManager.getInstance().apiService.invitePoster()
    }

    fun basketballIndex(map: MutableMap<String, Any>): Observable<BaseResult<MallSaleInfo>> {
        return RetrofitManager.getInstance().apiService.basketballIndex(map)
    }

    fun basketballBuyRate(): Observable<BaseResult<RateInfo>> {
        return RetrofitManager.getInstance().apiService.basketballBuyRate()
    }

    fun basketballBuy(map: MutableMap<String, Any>): Observable<BaseResult<Any?>> {
        return RetrofitManager.getInstance().apiService.basketballBuy(getRequestBody(map))
    }

    fun basketballBuySell(map: MutableMap<String, Any>): Observable<BaseResult<Any?>> {
        return RetrofitManager.getInstance().apiService.basketballBuySell(getRequestBody(map))
    }

    fun basketballLog(map: MutableMap<String, Any>): Observable<BaseResult<List<MallRecordInfo>?>> {
        return RetrofitManager.getInstance().apiService.basketballLog(map)
    }

    fun memberBasketBall(map: MutableMap<String, Any>): Observable<BaseResult<PackInfo>> {
        return RetrofitManager.getInstance().apiService.memberBasketBall(map)
    }

    fun basketballSell(id: String): Observable<BaseResult<PackBallSellInfo>> {
        return RetrofitManager.getInstance().apiService.basketballSell(id)
    }

    fun basketballSellConfirm(map: MutableMap<String, Any>): Observable<BaseResult<Any?>> {
        return RetrofitManager.getInstance().apiService.basketballSellConfirm(getRequestBody(map))
    }

    fun basketballSellCancel(map: MutableMap<String, Any>): Observable<BaseResult<Any?>> {
        return RetrofitManager.getInstance().apiService.basketballSellCancel(getRequestBody(map))
    }

    fun getSyntheticList(): Observable<BaseResult<List<SyntheticBallInfo>?>> {
        return RetrofitManager.getInstance().apiService.getSyntheticList()
    }

    fun getNotice(): Observable<BaseResult<Notice>> {
        return RetrofitManager.getInstance().apiService.getNotice()
    }

    fun getGuessLog(map: MutableMap<String, Any>): Observable<BaseResult<List<Guess>>> {
        return RetrofitManager.getInstance().apiService.getGuessLog(map)
    }

    fun getGuessInfoList(): Observable<BaseResult<GuessInfo>> {
        return RetrofitManager.getInstance().apiService.getGuessInfoList()
    }


    fun getSyntheticInfo(id: String): Observable<BaseResult<SyntheticInfo>> {
        return RetrofitManager.getInstance().apiService.getSyntheticInfo(id)
    }

    fun getSyntheticGuessInfo(id: String): Observable<BaseResult<SyntheticGuessInfo>> {
        return RetrofitManager.getInstance().apiService.getSyntheticGuessInfo(id)
    }

    fun getuploadToken(): Observable<BaseResult<QINiuToken>> {
        return RetrofitManager.getInstance().apiService.getuploadToken()
    }
//    fun getVersion(id: String): Observable<BaseResult<Version>> {
//        return RetrofitManager.getInstance().apiService.getVersion(id)
//    }
    fun getBottlesalvage(map: MutableMap<String, Any>): Observable<BaseResult<BottleDetail>> {
        return RetrofitManager.getInstance().apiService.getBottlesalvage(getRequestBody(map))
    }

    fun getBottle(id: String): Observable<BaseResult<BottleDetail>> {
        return RetrofitManager.getInstance().apiService.getBottle(id)
    }

    fun seeBottle(id: String): Observable<BaseResult<BottleDetail>> {
        return RetrofitManager.getInstance().apiService.seeBottle(id)
    }

    fun getCanGetNum(): Observable<BaseResult<BottleCount>> {
        return RetrofitManager.getInstance().apiService.getCanGetNum()
    }

    fun syntheticSubmit(map: MutableMap<String, Any>): Observable<BaseResult<Any?>> {
        return RetrofitManager.getInstance().apiService.syntheticSubmit(getRequestBody(map))
    }

    fun syntheticGuess(map: MutableMap<String, Any>): Observable<BaseResult<Any?>> {
        return RetrofitManager.getInstance().apiService.syntheticGuess(getRequestBody(map))
    }

    fun syntheticRecord(map: MutableMap<String, Any>): Observable<BaseResult<List<SyntheticRecordInfo>?>> {
        return RetrofitManager.getInstance().apiService.syntheticRecord(map)
    }

    fun winRecord(map: MutableMap<String, Any>): Observable<BaseResult<List<SyntheticRecordInfo>?>> {
        return RetrofitManager.getInstance().apiService.winRecord(map)
    }


    fun taskLog(map: MutableMap<String, Any>): Observable<BaseResult<List<PlayCountRecordInfo>?>> {
        return RetrofitManager.getInstance().apiService.taskLog(map)
    }

    fun task(): Observable<BaseResult<PlayCountInfo?>> {
        return RetrofitManager.getInstance().apiService.task()
    }

    fun taskSubmit(map: MutableMap<String, Any>): Observable<BaseResult<Any?>> {
        return RetrofitManager.getInstance().apiService.taskSubmit(getRequestBody(map))
    }

    fun gas(): Observable<BaseResult<GasInfo?>> {
        return RetrofitManager.getInstance().apiService.gas()
    }

    fun gasSubmit(): Observable<BaseResult<Any?>> {
        return RetrofitManager.getInstance().apiService.gasSubmit()
    }

    fun claimedLog(map: MutableMap<String, Any>): Observable<BaseResult<List<ClaimedInfo>?>> {
        return RetrofitManager.getInstance().apiService.claimedLog(map)
    }

    fun getTransferInfo(): Observable<BaseResult<TransferInfo>> {
        return RetrofitManager.getInstance().apiService.getTransferInfo()
    }

    fun commitTransfer(map: MutableMap<String, Any>): Observable<BaseResult<Any?>> {
        return RetrofitManager.getInstance().apiService.commitTransfer(getRequestBody(map))
    }
    fun getUserInfo(): Observable<BaseResult<UserBean>> {
        return RetrofitManager.getInstance().apiService.getUserInfo()
    }

    fun putBottle(map: MutableMap<String, Any>): Observable<BaseResult<Any?>> {
        return RetrofitManager.getInstance().apiService.putBottle(getRequestBody(map))
    }

    fun getBottleWater( map: MutableMap<String, Any>): Observable<BaseResult<List<BottleList>>> {
        return RetrofitManager.getInstance().apiService.getBottleWater(getRequestBody(map))
    }

    fun addAvatar(map: MutableMap<String, Any>): Observable<BaseResult<Any?>> {
        return RetrofitManager.getInstance().apiService.addAvatar(getRequestBody(map))
    }
    fun addName(map: MutableMap<String, Any>): Observable<BaseResult<Any?>> {
        return RetrofitManager.getInstance().apiService.updateName(getRequestBody(map))
    }

    fun addComment(map: MutableMap<String, Any>): Observable<BaseResult<Any?>> {
        return RetrofitManager.getInstance().apiService.addComment(getRequestBody(map))
    }

    fun getBottleList(pn: Int,count: Int): Observable<BaseResult<List<BottleListData>?>> {
        return RetrofitManager.getInstance().apiService.getBottleList(pn,count)
    }
    fun getPrivateGroup(bottle_id:Int,ids:MutableList<String>): Observable<BaseResult<PImGroup>> {
        val map = mutableMapOf<String, Any>()
        map["target_ids"] = ids
        map["bottle_id"]=bottle_id
        map["type"]=1
        return RetrofitManager.getInstance().apiService.getPrivateGroup(getRequestBody(map))
    }
    fun getPrivateGroupStatus(roomIds:MutableList<String>): Observable<BaseResult<MutableMap<String,List<PImGroupStatus>>>> {
        val map = mutableMapOf<String, Any>()
        map["room_ids"] = roomIds
        return RetrofitManager.getInstance().apiService.getPrivateGroupStatus(getRequestBody(map))
    }
    //公开身份
    fun updateOpenId(roomId:String): Observable<BaseResult<Any>> {
        val map = mutableMapOf<String, Any>()
        map["room_id"] = roomId
        return RetrofitManager.getInstance().apiService.updateOpenId(getRequestBody(map))
    }



    fun likeComment(id: String): Observable<BaseResult<Any?>> {
        return RetrofitManager.getInstance().apiService.likeComment(id)
    }
    fun unlikeComment(id: String): Observable<BaseResult<Any?>> {
        return RetrofitManager.getInstance().apiService.unlikeComment(id)
    }

    fun read(id: String): Observable<BaseResult<Any?>> {
        return RetrofitManager.getInstance().apiService.read(id)
    }

    fun follow(id: String): Observable<BaseResult<Any?>> {
        return RetrofitManager.getInstance().apiService.follow(id)
    }

    fun unfollow(id: String): Observable<BaseResult<Any?>> {
        return RetrofitManager.getInstance().apiService.unfollow(id)
    }

    fun reportBottle(map: MutableMap<String, Any>): Observable<BaseResult<Any?>> {
        return RetrofitManager.getInstance().apiService.reportBottle(getRequestBody(map))
    }


    fun followBottle(id: String): Observable<BaseResult<Any?>> {
        return RetrofitManager.getInstance().apiService.followBottle(id)
    }

    fun unfollowBottle(id: String): Observable<BaseResult<Any?>> {
        return RetrofitManager.getInstance().apiService.unfollowBottle(id)
    }

    fun deleteComment(id: String): Observable<BaseResult<Any?>> {
        return RetrofitManager.getInstance().apiService.deleteComment(id)
    }

}